package com.atguigu.ggkt.vod.service.impl;

import com.atguigu.ggkt.exception.GgktException;
import com.atguigu.ggkt.vod.service.VodService;
import com.qcloud.vod.VodUploadClient;
import com.qcloud.vod.model.VodUploadRequest;
import com.qcloud.vod.model.VodUploadResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.DeleteMediaRequest;
import com.tencentcloudapi.vod.v20180717.models.DeleteMediaResponse;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.crypto.modes.gcm.GCMExponentiator;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.io.InputStream;
@Slf4j
@Service
public class VodServiceImpl implements VodService {
    @Override
    public String uploadVideo(InputStream inputStream, String originalFilename) {
        VodUploadClient client = new VodUploadClient("AKIDiFagW6QsHTP4TDj13pMjqjMlvpVwBC3h", "UihmgLkESRFm3C5h38VheOvGOJ4cv0qS");
        VodUploadRequest request = new VodUploadRequest();

        log.info("视频路径{}",originalFilename);
        request.setMediaFilePath("D:\\Xshell7\\"+originalFilename); //要上传的文件视频
        request.setProcedure("LongVideoPreset"); //任务流名称
        try {
            //地域
            VodUploadResponse response = client.upload("ap-beijing", request);
            log.info("Upload FileId = {}", response.getFileId());
            String fileId = response.getFileId();
            return fileId;
        } catch (Exception e) {
            // 业务方进行异常处理
            log.error("Upload Err", e);
            throw new GgktException(20001,"上传失败");
        }
    }

    //删除视频
    @Override
    public void removeVideo(String videoSourceId) {
        try{
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            Credential cred =
                    new Credential("AKIDiFagW6QsHTP4TDj13pMjqjMlvpVwBC3h",
                            "UihmgLkESRFm3C5h38VheOvGOJ4cv0qS");
            // 实例化要请求产品的client对象,clientProfile是可选的
            VodClient client = new VodClient(cred, "");
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DeleteMediaRequest req = new DeleteMediaRequest();
            req.setFileId(videoSourceId);
            // 返回的resp是一个DeleteMediaResponse的实例，与请求对象对应
            DeleteMediaResponse resp = client.DeleteMedia(req);
            // 输出json格式的字符串回包
            System.out.println(DeleteMediaResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
    }
}
